# coding=utf-8

import os  # 建立本地目录使用
import time
import requests
import re

# 图片url：https://p5.manhuapan.com/21/Vol_01/001aa.jpg
# 网址url：https://manhua.fzdm.com/21/Vol_01/index_0.html

pic_url_header = 'https://p5.manhuapan.com/21/'   # 图片连接头部， 从256话后就不一样了
page_url_header = 'https://manhua.fzdm.com/21/'   # 网址头部

page_url_mid = 'Vol_01'                           # 网址中间部分
page_url_html = 'index_0'                        # 网址尾巴
page_next = page_url_header + page_url_mid + '/' + page_url_html + '.html'
# print(page_next)                                # https://manhua.fzdm.com/21/256/index_0.html
is_end = 1                                        # 判断是否结束，0表示结束，
count = 0                                           # 50次后停止10秒
time_start = time.time()
time_during = 600.0
time_end = time_start + time_during

while True:
    if time.time() < time_end:
        response_html = requests.get(page_next, timeout=15)
        response = response_html.text
        # print(response)   # var Clid="21";var mhurl="2007/09/085540401.jpg"

        # 获取图片url
        pic_url_mid_list = re.findall('var mhurl=\"21\/(.*?).jpg', response)   # ['Vol_01/001aa']，从256话后就不能使用了
        pic_url_mid = pic_url_mid_list[0]
        pic_url = pic_url_header + pic_url_mid + '.jpg'
        # 获取文件路径
        pic_file_mid, pic_file_name = pic_url_mid.split('/')
        print(pic_file_mid, pic_file_name)  # Vol_01 001aa

        # 图片存放路径
        pic_file_path = os.getcwd() + os.sep + page_url_mid + os.sep + page_url_html + '.jpg'  # D:\Users\asus-pc\PycharmProjects\comic_download\256\index_0.jpg

        # 创建文件夹
        if not os.path.exists(os.getcwd() + os.sep + page_url_mid):
            os.mkdir(os.getcwd() + os.sep + page_url_mid)

        print('图片src: %s' % pic_url)
        # 将图片写入到
        while not os.path.exists(pic_file_path):
            print('计数器:%s' % count)
            print('正在将图片下载到>>>>>>>>>>%s' % pic_file_path)
            try:
                pic_response = requests.get(pic_url)  # 获取网页
                print('请求图片状态码: %s' % pic_response.status_code)
                with open(pic_file_path, 'wb') as f:  # 打开写入到path路径里-二进制文件，返回的句柄名为f
                    f.write(pic_response.content)  # 往f里写入r对象的二进制文件
                f.close()
                pic_response.close()
                print('图片下载成功：%s' % pic_url)
            except requests.exceptions.RequestException as e:
                print('图片下载失败,重试:%s, %s' % (pic_url, e))
            # 设置停止时间，不要给服务器太大压力
            if count >= 100:
                print('*******************已经下载了100张图片，暂停10秒************************')
                time.sleep(10)
                count = 0
            count = count + 1

        """
        下一页
        <a href="index_2.html" class="pure-button pure-button-primary">下一页</a>
        """
        page_next_list = re.findall('<a href="(.*?)" class=', response)
        page_next_temp = page_next_list[-1]
        # print(page_next_list)
        print(type(page_next_temp), page_next_temp)
        # print(page_url_header)
        if page_next_temp.startswith('../'):
            page_next = page_url_header + page_next_temp[2:]
        else:
            page_next = page_url_header + '/' + page_next_temp

        print('图片url：%s' % page_next)
        response_html.close()
        # time.sleep(10)
        if is_end == 0:
            break
        if page_next == 'https://manhua.fzdm.com/21/G16/index_35.html':             # 最后一张图片
            is_end = 0
        print('-------------------------分隔符-----------------------------------')
    else:
        print('**********已经运行了10分钟，暂停30秒****************')
        time.sleep(30)
        time_end = time.time() + time_during






